package com.wangbo.box.decorator;

/**
 * 装饰模式
 *
 * 指在不改变现有对象结构的情况下，动态地给该对象增加一些职责（即增加其额外功能）
 */
public class DecoratorPattern {

    interface Subject{
        void show();
    }

    static class SubjectA implements Subject{

        @Override
        public void show() {
            System.out.println("subject A");
        }
    }

    static class Decorator implements Subject{

        Subject subject;

        public Decorator(Subject subject) {
            this.subject = subject;
        }

        @Override
        public void show() {
            subject.show();
        }
    }

    static class DecoratorA extends Decorator{

        public DecoratorA(Subject subject) {
            super(subject);
        }

        @Override
        public void show() {
            super.show();
            addedFunction();
        }

        private void addedFunction(){
            System.out.println("Decorator A Function");
        }
    }

    public static void main(String[] args) {
        Subject subject = new SubjectA();
        Subject decoratorSubject = new DecoratorA(subject);
        subject.show();
        decoratorSubject.show();
    }
}
